//
//  ScorllPictureView.m
//  u76ho
//
//  Created by m-air-01 on 15/12/29.
//  Copyright © 2015年 LV. All rights reserved.
//

#import "ScorllPictureView.h"
#import "PicCollectionViewCell.h"
@interface ScorllPictureView ()
{
    UICollectionView * _collectionView;
    
    NSTimer * timer;
    NSInteger times;//触发timer的次数
    NSInteger shouldSentNumber;//触发timer后当前图片的位置

    UIPageControl * pageController;
    CGSize size;
}
@end
@implementation ScorllPictureView
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self main];
        size = frame.size;
    }
    return self;
}
-(void)setPicArr:(NSArray *)picArr
{
    _picArr = picArr;
    pageController.numberOfPages = _picArr.count;
}
-(void)main
{
    UICollectionViewFlowLayout  * flow = [[UICollectionViewFlowLayout alloc]init];
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flow.minimumInteritemSpacing = 0;
    flow.minimumLineSpacing = 0;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.frame.size.height) collectionViewLayout:flow];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    [_collectionView registerNib:[UINib nibWithNibName:@"PicCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PicCollectionViewCell"];
    [self addSubview:_collectionView];
    
    [_collectionView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
    pageController = [[UIPageControl alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-70, self.frame.size.height-20, 50, 20)];
    [self addSubview:pageController];
    
    if (!timer) {
        
        timer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(timeClick) userInfo:nil repeats:YES];
    }
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x<=0) {
        [scrollView setContentOffset:CGPointMake(self.frame.size.width*(_picArr.count-2), 0) animated:NO];
    }
    if (scrollView.contentOffset.x>=(_picArr.count-1)*self.frame.size.width) {
        [scrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
    }
    if (scrollView.contentOffset.x>=self.frame.size.width&&scrollView.contentOffset.x<=(_picArr.count-2)*self.frame.size.width) {
        
        //        bomLabel.text = [NSString stringWithFormat:@"%.f/%d",scrollView.contentOffset.x/self.frame.size.width,(unsigned)_picArray.count-2];
//        NSLog(@"%f",(int)scrollView.contentOffset.x/self.frame.size.width);
    }
    else{
        //    bomLabel.text = [NSString stringWithFormat:@"%.f/%d",scrollView.contentOffset.x/SELF_WIDTH+1,(unsigned)_picArray.count];
//         NSLog(@"%f",(int)scrollView.contentOffset.x/self.frame.size.width);
    }
    

    pageController.currentPage = times-1;

}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x>=self.frame.size.width&&scrollView.contentOffset.x<=(_picArr.count-2)*self.frame.size.width) {
        times = [[NSString stringWithFormat:@"%.f", scrollView.contentOffset.x/self.frame.size.width] integerValue]-1;
        shouldSentNumber = times;
        pageController.currentPage = times-1;
        NSLog(@"%f",(int)scrollView.contentOffset.x/self.frame.size.width);
        [_collectionView setContentOffset:CGPointMake(SCREEN_WIDTH*times+SCREEN_WIDTH, 0) animated:YES];

//        NSLog(@"%f",(int)scrollView.contentOffset.x/self.frame.size.width);
        
    }

}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _picArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PicCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PicCollectionViewCell" forIndexPath:indexPath];
    if (_picArr.count) {
        id d = _picArr[indexPath.item];
        
        if ([d isKindOfClass:[NSString class]]) {
            if ([d rangeOfString:@"http"].length) {
                [cell.picImgView sd_setImageWithURL:[NSURL URLWithString:(NSString *)d] placeholderImage:nil];
            }
            else
            {
                cell.picImgView.image = [UIImage imageNamed:(NSString *)d];
            }
        }
    }
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate scrollPictureSelectedAt:(int)indexPath.item];
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return size;
}
-(void)refreshData
{
    NSMutableArray * tempArr = [[NSMutableArray alloc]initWithArray:_picArr];
    if (_picArr.count) {
        [tempArr insertObject:_picArr.lastObject atIndex:0];
        [tempArr addObject:_picArr.firstObject];
    }
    _picArr = tempArr;
    [_collectionView reloadData];
}
- (void)timeClick{
    
    //    0123
    if (times==_picArr.count-2) {
        times = 1;
    }else{
        times ++;
    }
    [_collectionView setContentOffset:CGPointMake(SCREEN_WIDTH*times+SCREEN_WIDTH, 0) animated:YES];
    
    //    不同的计算逻辑
    if (shouldSentNumber==_picArr.count-3) {
        shouldSentNumber = 0;
    }else{
        shouldSentNumber ++;
    }
    
    pageController.currentPage = times-1;

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
